home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 325 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.6 KB

  1. From: Dietmar Kuehl <Dietmar.Kuehl@uni-konstanz.de>
  2. Message-ID: <199602112213.XAA12589@uzwil>
  3. X-Original-Date: Sun, 11 Feb 1996 23:13:09 +0100 (MET)
  4. Path: in2.uu.net!bounce-back
  5. Date: 12 Feb 96 07:17:28 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Subject: Re: proposal: renew & relocator member fn
  9. Reply-To: Dietmar.Kuehl@uni-konstanz.de
  10. Newsgroups: comp.std.c++
  11. X-Mailer: ELM [version 2.4 PL25]
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMR7zmeEDnX0m9pzZAQGYnAF/djDtEyTa/8neazM0wpt9FcSbPYSBla6e
  14.     xxPNd5Md0lKRSUHcYw/h+26zMkXWHyDW
  15.     =5vRa
  16.  
  17. Hi,
  18.  
  19. Gregory Bond <gnb@bby.com.au> wrote:
  20. > People often ask for a "renew[]" operator to complement "new[]" in the
  21. > same way realloc() complements malloc().  Of course the problem there
  22. > is contructing the new objects and destructing the old ones, so a
  23. > whole bunch of code gets written to do a new[], a bunch of assigns and a
  24. > delete[]. This (or the moral equivalent using malloc(), placement new
  25. > and delete) is especially prevalent inside container classes such
  26. > as STL.
  27.  
  28. I can only see one (minor) advantage to use 'realloc()' instead of a
  29. more flexible method (see below): You can make use of the size of
  30. memory object known to the memory system. If you accept that you
  31. "waste" the storage to remember the actual size of a memory object you
  32. can do without 'renew' or 'realloc()' using a "traits" mechanism.
  33.  
  34. > I was thinking about the efficiency concerns here.  For what is
  35. > conceptually a simple operation, three calls are made - a default
  36. > ctor, a copy ctor and a dtor.  The copy ctor may potentially have to
  37.  
  38. Most likely, two operations are to be made: a placement call to the
  39. ctor and a call to the dtor. It should be avoided to construct an
  40. object twice (I'm not sure but I think constructing an object twice
  41. results in undefined behavior)!
  42.  
  43. > allocate and deep-copy extensive data structures, so this can be very
  44. > expensive, especially when shallow copy would be sufficient if the
  45. > copy-ctor could KNOW that the source object was about to be destroyed.
  46. > So it seems to me that a combination of copy constructor and destructor
  47. > could be used in these circumstances to "relocate" an object.  You
  48. > could call this special member a "relocator".  This could offer
  49. > substantial efficiency gains, especially in situations like
  50. > vector<vector<A> >.
  51.  
  52. I agree with you that the copy constructor is sometimes a bad choice to
  53. be used for doing a reallocation. A more appropriate constructor, a
  54. "move constructor", can often be used: This constructor initializes one
  55. object by moving all relevant members from one object into another one
  56. leaving the original object in a state which is useless except for
  57. destruction (this sometimes involves additional operations like setting
  58. pointers to zero). This makes exactly use of the knowledge you want to
  59. use for your "relocator" function. All what remains is to tell a
  60. container class which does the actual relocation that it should use a
  61. pair of "move constructor" and destructor calls. One possibility to do
  62. so is to gather this information and other relevant information in a
  63. "Traits" template argument to the container which contains 'static'
  64. member functions to be used for certain operations.  Here are general
  65. and specific "traits" which define a method for a movement:
  66.  
  67.   // The general 'move_traits' are useful for all classes defining
  68.   // a copy constructor. Classes with other needs can define
  69.   // specializations of this struct.
  70.  
  71.   template <class T>
  72.   struct move_traits
  73.   {
  74.     T *move(T &source, void *target)
  75.     {
  76.       T *rc = new(target) T(source);
  77.       source.~T();
  78.       return rc;
  79.     }
  80.   };
  81.  
  82.   class Moveable
  83.   {
  84.   public:
  85.     // pass a 'bool' for a different signature than the copy ctor
  86.     Moveable(Moveable &, bool);
  87.     // ...
  88.   };
  89.  
  90.   struct move_traits<Moveable>
  91.   {
  92.     T *move(T &source, void *target)
  93.     {
  94.       T *rc = new(target) T(source, true);
  95.       source.~T();
  96.       return rc;
  97.     }
  98.   };
  99.  
  100. This solution of the move problem might require more work when
  101. implementing a "moveable" class than the 'renew' approach. But is does
  102. not require an extension of the language which is large enough without
  103. 'renew'. A problem might be that the STL containers do not use this
  104. approach to move an object but I think this could be solved easily if
  105. it is really desired. For an example of a container class (which is not
  106. completely finished) using such an approach you can have a look at
  107.  
  108.   http://www.informatik.uni-konstanz.de/~kuehl/c++/array.h.html
  109.  
  110. Another approach is to use smart-pointers: Use a smart-pointer with
  111. "move on copy" semantic (like 'auto_ptr'; is it defined like this in
  112. the current version of the DWP?) or a "copy on write" semantic
  113. involving some reference counting. Instead of copying the whole object,
  114. a pointer to the object is copied. Unfortunately, the Standard Library
  115. does not include a complete set of smart pointers. The only class of
  116. this category is 'auto_ptr' with rather controversive semantics. This
  117. is probably the reason why there are no other smart pointers included:
  118. It is hard to find a reasonable consensus what they should do...
  119.  
  120. In general, I don't see why a 'renew' operator would be necessary.
  121. Although it wouldn't cost anything for the portions of the code which
  122. do not use it, it does not solve any problem which cannot be solved
  123. with the current definition of the language.
  124. -- 
  125. dietmar.kuehl@uni-konstanz.de
  126. http://www.informatik.uni-konstanz.de/~kuehl
  127. I am a realistic optimist - that's why I appear to be slightly pessimistic
  128. ---
  129. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  130.   Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  131.   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  132.